<?php
	function getLastNPlati($db, $tip, $perPage, $offset, $orderBY, $orderKIND, $idClient, $moneda = "", $suma = "", $nr_comanda = "", $data = "") {
		global $totals;        
		//forge the WHERE part of the query		
		$WHERE = " WHERE tip = " . $tip . " AND plati_comanda.deleted = " . CT_NOT_DELETED;
		
		if ($suma != "") $WHERE .= " AND ROUND(suma,2) = ROUND($suma , 2)";
		//indiferent ca este o incasare sau o sortare, cautarea dupa numarul comenzii are in vedere numarul_comenzii_executantului, adica comanta mea!
        if ($nr_comanda != "") 
				$WHERE .= " AND nr_comanda_exec LIKE '%$nr_comanda%' ";
		if ($data != "") $WHERE .= " AND data = '$data' ";
		if ($moneda != "") $WHERE .= " AND moneda = $moneda";
        $WHERE .= " AND comanda.id_client = $idClient ";
		$ORDER = " ORDER BY ";
		if ($orderBY != "") {
			$ORDER .= $orderBY . " ";
			if ($orderKIND == "1") $ORDER .= " ASC ";
			else $ORDER .= " DESC ";
		}
		else $ORDER .= " data DESC ";
		//end forge the WHERE part of the query
		$query = "SELECT SQL_CALC_FOUND_ROWS plati_comanda.id, moneda, suma, data, comanda.nr_comanda_exec FROM plati_comanda 
						LEFT JOIN comanda
						ON plati_comanda.id_comanda = comanda.id " . 
						$WHERE . 
						$ORDER . " 						
						LIMIT " . $offset . ", " . $perPage;
//        echo ($query);
		$result = $db->query($query);								
		
		if ($result === FALSE) {    	  	
			throw new QueryGetLast20PlatiException("Failed fetching last 20 plati.");
		}													

		$plati = array();
		while ($row = $result->fetch(PDO::FETCH_ASSOC))
			$plati[] = $row;
			
		$query = "SELECT FOUND_ROWS() AS totals";
		$result = $db->query($query);								
		if ($result === FALSE) {			
    	  	throw new QueryGetLast20PlatiException("Failed fetching last 20 plati.");
		}
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$totals = $row['totals'];			
			
		return $plati;
	}
	
	function renderPagination($tip, $perPage, $defaultView, $pageNumber) {
		global $totals;		
        $totalPages = ceil($totals / $perPage);
                       
        if ($totalPages >= 10)
            $maxPagesDisplayed = 10;
        else 
            $maxPagesDisplayed = $totalPages;
            
        $style = 'style="font-weight: bold;"';
        $currentStyle = "";
                             
		if ($totalPages > 1) {			
			echo '<tr class="paginare">';
			    echo '<td colspan="5" align="center">';					    
				    echo LANG_Pagina . " ";	
                    if ($i + 1 == $pageNumber)		
                        $currentStyle = $style;
                    echo '<a ' . $currentStyle . ' href="javascript: renderPlatiPage(\'' . $tip . '\', \'' . (1) . '\', \'' . $perPage . '\', \'' . $defaultView . '\', orderBY' . ', orderKIND);">' . (1) . "</a> ";
                    $currentStyle == "";                        
                    if ($pageNumber >= 6) {
                        if ($pageNumber != 6) 
                            echo " ... ";
                        $upLimit = $pageNumber + 5;
                        if ($upLimit > $totalPages) $upLimit = $totalPages;
                        
                        for ($i = ($pageNumber - 5); $i < $upLimit; $i++) {
                            if ($i + 1  == $pageNumber) 
                                $currentStyle = $style;
                            echo '<a ' . $currentStyle . ' href="javascript: renderPlatiPage(\'' . $tip . '\', \'' . ($i+1) . '\', \'' . $perPage . '\', \'' . $defaultView . '\', orderBY' . ', orderKIND);">' . ($i + 1) . "</a> ";
                            $currentStyle = "";
                        }                        
                    }
                    else {
				        for ($j = 1; $j < $maxPagesDisplayed - 1; $j++) {	
                            $currentStyle = "";
                            if ($j + 1 == $pageNumber) 
                                $currentStyle = $style;		
					        echo '<a ' . $currentStyle . ' href="javascript: renderPlatiPage(\'' . $tip . '\', \'' . ($j+1) . '\', \'' . $perPage . '\', \'' . $defaultView . '\', orderBY' . ', orderKIND);">' . ($j + 1) . "</a> ";
				        }
                    } 
                    if ($i != $totalPages) {
                        if  ($pageNumber <= ($totalPages - 7))
                            echo " ... ";                        
                        echo '<a href="javascript: renderPlatiPage(\'' . $tip . '\', \'' . ($totalPages) . '\', \'' . $perPage . '\', \'' . $defaultView . '\', orderBY' . ', orderKIND);">' . ($totalPages) . "</a> ";
                    }                    
                echo '</td>';
                echo '<td>';
                    echo LANG_GO_TO;
                    echo '<input type="text" size="1" name="gotopag" id="gotopag" value="' . $pageNumber . '"/>';                    
                    echo '<input type="button" onclick="javascript: if (gotoPage(document.getElementById(\'gotopag\').value, ' . $totalPages . ')) renderPlatiPage(\'' . $tip . "', document.getElementById('gotopag').value," . '\'' . $perPage . '\', \'' . $defaultView . '\', orderBY' . ', orderKIND);" name="gotopagbutton" value="' . LANG_OK . '" />';
                echo '</td>';
			echo '</tr>';
		}
	}
	
	function print_ASC_DESC_image($order) {
		global $orderBY;
		global $orderKIND;
		
		if ($orderBY == $order) 
			if ($orderKIND == 1) echo '<img src="images/sortA.jpg" />';										
			else echo '<img src="images/sortD.jpg" />';										
	}
    
    function returnBGColor($order) {
        global $orderBY;
        if ($order == $orderBY) return 'bgcolor="' . CT_COLOR_HIGHLIGHT_SORT_COLUMN . '"';
        else return "";
    }
	
	function renderPlatiTable($tip, $platiArray, $perPage, $defaultView, $pageNumber, $numarComanda = "") {						
		global $totals;
		global $currencies;
        
        
		echo '<table align="left" id="wrapper">';
			echo '<thead>';
				echo '<tr>';
				echo '<th colspan="6" align="center">';					
					if ($tip == 1) echo LANG_Tabel_Plati;
					else echo LANG_Tabel_Incasari;
				echo '</th>';
				echo '</tr>';
			echo '</thead>';	
					
			renderPagination($tip, $perPage, $defaultView, $pageNumber);					
			
			echo '<tr class="capTabel">';
				echo '<td align="left" ' . returnBGColor("nr_comanda_exec") . ' class="th_plati" onclick="javascript: sortByHeading(\'' . $tip . '\', \'' . $pageNumber . '\', \'' . $perPage . '\', \'' . $defaultView . '\', ', '\'nr_comanda_exec\', ' . '\'0\');"><b>'.LANG_NR_COMANDA. '</b> ';
				print_ASC_DESC_image("nr_comanda_exec");
				echo '</td>';
				
				echo '<td align="left"' . returnBGColor("suma") . ' class="th_plati" onclick="javascript: sortByHeading(\'' . $tip . '\', \'' . $pageNumber . '\', \'' . $perPage . '\', \'' . $defaultView . '\', ', '\'suma\', ' . '\'0\');"><b>'.LANG_SUMA.'</b> ';
				print_ASC_DESC_image("suma");
				echo '</td>';
				
				echo '<td align="left"' . returnBGColor("moneda") . ' class="th_plati" onclick="javascript: sortByHeading(\'' . $tip . '\', \'' . $pageNumber . '\', \'' . $perPage . '\', \'' . $defaultView . '\', ', '\'moneda\', ' . '\'0\');"><b>'.LANG_Moneda.'</b> ';
				print_ASC_DESC_image("moneda");
				echo '</td>';
								
				echo '<td align="left"' . returnBGColor("data") . ' class="th_plati" onclick="javascript: sortByHeading(\'' . $tip . '\', \'' . $pageNumber . '\', \'' . $perPage . '\', \'' . $defaultView . '\', ', '\'data\', ' . '\'0\');"><b>'.LANG_DATA. '</b> ';
				print_ASC_DESC_image("data");
				echo '</td>';
				
				echo '<td class="th_plati"><b>'.LANG_EDIT.'</b></td>';
				echo '<td class="th_plati" align="center"><b>'.LANG_DELETE.'</b></td>';
			echo '</tr>';
			
			for ($i = 0; $i < count($platiArray); $i++) {                    
				echo '<tr onmouseout=this.className="whiteThing" onmouseover=this.className="blueThing" class="whiteThing">';
					echo '<td '. returnBGColor("nr_comanda_exec") . 'align="left" width="100px">'.$platiArray[$i]['nr_comanda_exec'].'</td>';
					echo '<td '. returnBGColor("suma") . 'align="left" width="60px">'.prepareFloatForUser($platiArray[$i]['suma']).'</td>';
					echo '<td '. returnBGColor("moneda") . 'align="left" width="70px">'.$currencies[$platiArray[$i]['moneda']].'</td>';
					echo '<td '. returnBGColor("data") . 'align="left">'.getDateForUser($platiArray[$i]['data']).'</td>';
					if ($tip == 1)
						echo '<td align="center"><input type="button" name="editPlata" value="Edit" onclick="javascript: editPlata(\'' . $platiArray[$i]['id'] . '\');" /></td>';					
					else 
						echo '<td align="center"><input type="button" name="editPlata" value="Edit" onclick="javascript: editIncasare(\'' . $platiArray[$i]['id'] . '\');" /></td>';					
					echo '<td align="center"><input type="button" name="deletePlata" value="Delete" onclick="javascript: deletePlata(\'' . $platiArray[$i]['id'] . '\', \'' . $defaultView . '\', \'' . $pageNumber . '\', \'' . $perPage . '\', \'' . $tip . '\', \'' . $numarComanda. '\');" /></td>';
				echo '</tr>';
			}
            if (count($platiArray) == 0) {
                echo '<tr class="capTabel">';
                    echo '<td colspan="6" align="center">';
                        if ($tip == 1)
                            echo '<b>' . LANG_NU_SUNT_PLATI . '</b>';
                        else
                            echo '<b>' . LANG_NU_SUNT_INCASASRI . '</b>';
                    echo '</td>';
                echo '</tr>';
            }			
		echo '</table>';		
	}
	
	function render_status_comanda_table($nrComanda, $status, $suma_partiala, $rest_plata, $total, $moneda = "EUR") {		
		echo '<div class="container">';
			echo '<table id="wrapper" align="left">';
				echo '<thead>';
					echo '<tr>';
						echo '<th colspan="6">';
							echo '<b>' . LANG_INFORMATII_COMANDA . '</b>';
						echo '</th>';
					echo '</tr>';
				echo '</thead>';
				echo '<tr class="capTabel">';
					echo '<td>';
						echo '<b>' . LANG_NR_COMANDA . '</b>';
					echo '</td>';
					echo '<td>';
						echo '<b>' . LANG_STATUS_PLATA . '</b>';
					echo '</td>';
					echo '<td>';
						echo '<b>' . LANG_PLATA_PARTIALA . '</b>';
					echo '</td>';
					echo '<td>';
						echo '<b>' . LANG_REST_PLATA . '</b>';
					echo '</td>';
					echo '<td>';
						echo '<b>' . LANG_TOTAL . '</b>';
					echo '</td>';
                    echo '<td>';
                        echo '<b>' . LANG_Moneda . '</b>';
                    echo '</td>';
				echo '</tr>';
				echo '<tr onmouseout=this.className="whiteThing" onmouseover=this.className="blueThing" class="whiteThing">';
					echo '<td>';
						echo $nrComanda;
					echo '</td>';									
					echo '<td>';
						echo '<b>' . $status . '</b>';
					echo '</td>';
					echo '<td>';
						echo prepareFloatForUser($suma_partiala);
					echo '</td>';
					echo '<td>';
						echo prepareFloatForUser($rest_plata);
					echo '</td>';
					echo '<td>';
						echo prepareFloatForUser($total);					
					echo '</td>';
                    echo '<td>';
                        echo $moneda;
                    echo '</td>';
				echo '</tr>';
			echo '</table>';		
		echo '</div>';
	}
?>
